// subPages/mine/myActivities/myActivities.js
const app = getApp();
var util =  require('../../../utils/util.js');
var json =  require('../../../data/json.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = '活动'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navList: [
      {
        nav: '社区活动',
        img: '/resources/theme/my5.png',
        imgW: 32,
        imgH: 35,
      },
      {
        nav: '社区公益',
        img: '/resources/theme/my6.png',
        imgW: 34,
        imgH: 30,
      }
    ],
    current: 0,
    type: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(app.globalData.userId){
      that.setData({
        userId: app.globalData.userId
      })
      that.getData()
    }else{
      app.userIdCallback = userId => {
        if (userId != '') {
          that.setData({
            userId: userId
          });
          that.getData()
        }
      }
    }
  },

  //获取数据
  getData:function(){
    //获取商品数据
    util.getAjax(app.globalData.apiUrl +'/get_up_hd_gy_content',this.getActivitiesData,{type: this.data.type,user_id: this.data.userId,p: p,num: num},this.getDataErr)
  },

  getActivitiesData(res){
    var that = this
    // console.log(res)
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.activityList.concat(res.data.list)
      }
      for(var i in list){
        if(list[i].content.status == 0){
          var endTime = new Date(String(list[i].content.close_time).replace(/-/g,"/")).getTime() || [];//倒计时的结束时间
          var nowTime = (new Date(util.formatTime(new Date()).substring(0,10)).getTime())
          var total_micro_second = endTime - nowTime
          if (total_micro_second <= 0) {
            list[i].content.status = 1
          }
        }
      }
      that.setData({
        isActivity:true,
        activityList: list
      })
    }else{
      that.setData({
        isActivity:false,
        activityList: []
      })
    }
    loadMoreView = that.selectComponent("#loadMoreView")
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  shopDataErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取活动失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    this.getData()
  },

  clickLoadMore: function(e) {
    this.getData()
  },

  //选择导航栏
  switchNav(event){
    var that = this
    var cur = event.currentTarget.dataset.current;
    if (that.data.current == cur) {
      return false;
    } else {
      var type = ''
      switch (cur) {
        case 0:
          type = 1
          break;
        case 1:
          type = 2
          break;
      }
      that.setData({
        current: cur,
        type: type
      })
      p=1
      this.getData()
    }
  },

  //立即参与
  activitybtn:function(){
    if(this.data.type == 1){
      wx.navigateTo({
        url: '/subPages/center/communityActivities/communityActivities',
      })
    }else if(this.data.type ==2){
      wx.navigateTo({
        url: '/subPages/center/communityPublicWelfare/communityPublicWelfare',
      })
    }
  },

  //报名
  signUp:function(e){
    var item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/subPages/center/signUp/signUp?list=' + JSON.stringify(item.content) + '&type=' + this.data.type,
    })
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.isFail){
      this.getData()
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})